/* $RCSFile: Options.ascx.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/AddonNice.ECommerce/Design/ProductLayouts/Options.ascx.cs $
 * $log$
 * Revision 21 2010/08/09 11:08:11  christian.surieux
 *   Added Log comment to all .ascx .aspx .master files
 * Revision 8 2010/08/09 10:07:05  christian.surieux
 *   Added Log comment to all cs files
 */

using System;
using System.Diagnostics;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Globalization;

using AddonNice.Diagnostics;

using Trace2 =   System.Diagnostics.Trace;

namespace AddonNice.ECommerce.Design
{

	/// <summary>
	///		Summary description for Options.
	/// </summary>
	public partial class Options : UserControl
	{
		// used to modify the SetOptions string
		protected XmlDocument xmlOptionsDoc;

		// This will accept an xmlText string and populate the option drop down
		private string _setOptions =	 string.Empty;
		public string SetOptions
		{
			get
			{
				// this will be used to get back the selected options
				// get the options as an XML document
				xmlOptionsDoc				=   GetOptionsXml();
				// no need to delete the existing selection (we do not have any)
				XmlNodeList nodeList		=   xmlOptionsDoc.GetElementsByTagName(EComStStr.ProductOptions);

				if( ddOptions1.SelectedItem != null )
				{
					XmlElement option		=   xmlOptionsDoc.CreateElement("option1");
					XmlElement optionValue	=   xmlOptionsDoc.CreateElement("selected");
					optionValue.InnerText	=   ddOptions1.SelectedItem.Value;
					option.AppendChild(optionValue);
					nodeList.Item(0).AppendChild(option);
				}

				if( ddOptions2.SelectedItem != null )
				{
					XmlElement option		=   xmlOptionsDoc.CreateElement("option2");
					XmlElement optionValue	=   xmlOptionsDoc.CreateElement("selected");
					optionValue.InnerText	=   ddOptions2.SelectedItem.Value;
					option.AppendChild(optionValue);
					nodeList.Item(0).AppendChild(option);
				}

				if( ddOptions3.SelectedItem != null )
				{
					XmlElement option		=   xmlOptionsDoc.CreateElement("option3");
					XmlElement optionValue	=   xmlOptionsDoc.CreateElement("selected");
					optionValue.InnerText	=   ddOptions3.SelectedItem.Value;
					option.AppendChild(optionValue);
					nodeList.Item(0).AppendChild(option);
				}

				if( ddOptions4.SelectedItem != null )
				{
					XmlElement option		=   xmlOptionsDoc.CreateElement("option4");
					XmlElement optionValue	=   xmlOptionsDoc.CreateElement("selected");
					optionValue.InnerText	=   ddOptions3.SelectedItem.Value;
					option.AppendChild(optionValue);
					nodeList.Item(0).AppendChild(option);
				}
			    string xmlString		    =   string.Empty;
			    XmlNode foundNode		    =   xmlOptionsDoc.SelectSingleNode(EComStStr.ProductOptions);
			    bool found  =   (foundNode != null);
			    if( found )
				    xmlString			    =   foundNode.OuterXml;
                if ( TraceProducts.Sw.TraceVerbose )
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]Options SetOptions GET xmlString: {0}, found: {1}",xmlString,found),TraceProducts.Sw.Info);
			    return xmlString;
			}
			set
			{
				_setOptions					= value; // save it
				SetOptionValues(value);
			}
		}

		//This will set the options title label
		public string OptionTitle
		{
			set
			{
				lblOptions.Text = value;
			}
		}

        /// <summary>
		/// provide a string representing an xml file or null for a new document
		/// </summary>
		protected void SetOptionValues(string myXmlString)
		{
            if ( TraceProducts.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]Options SetOptionValues BEG myXmlString: {0}.",myXmlString),TraceProducts.Sw.Info);
			XmlDocument myXmlDoc			= new XmlDocument();
			try
			{
				ddOptions1.Visible			= false;
				ddOptions2.Visible			= false;
				ddOptions3.Visible			= false;
				ddOptions4.Visible			= false;
				lblOptions.Visible			= false;

				if( !string.IsNullOrEmpty(myXmlString) )
				{
					//We will create the xml document from the data
					//Create our Xml Document from the db data
					myXmlDoc.LoadXml(myXmlString);
			
					//Now fill the Drop down Box: option 1
					ddOptions1.Items.Clear();
					XmlNodeList nodeList1	= myXmlDoc.GetElementsByTagName("option1");
					//Check if we need to hide the options
					if(!(nodeList1 == null || nodeList1.Count == 0))
					{
						foreach(XmlNode node in nodeList1)
							ddOptions1.Items.Add(node["value"].InnerText);

						ddOptions1.Visible	= true;
						lblOptions.Visible	= true;
					}
					//Now fill the Drop down Box: option 2
					ddOptions2.Items.Clear();
					XmlNodeList nodeList2	= myXmlDoc.GetElementsByTagName("option2");
					//Check if we need to hide the options
					if(!(nodeList2 == null || nodeList2.Count == 0))
					{
						foreach(XmlNode node in nodeList2)
							ddOptions2.Items.Add(node["value"].InnerText);

						ddOptions2.Visible	= true;
						lblOptions.Visible	= true;
					}
					//Now fill the Drop down Box: option 3
					ddOptions3.Items.Clear();
					XmlNodeList nodeList3	= myXmlDoc.GetElementsByTagName("option3");
					//Check if we need to hide the options
					if(!(nodeList3 == null || nodeList3.Count == 0))
					{
						foreach(XmlNode node in nodeList3)
							ddOptions3.Items.Add(node["value"].InnerText);

						ddOptions3.Visible	= true;
						lblOptions.Visible	= true;
					}
					//Now fill the Drop down Box: option 3
					ddOptions4.Items.Clear();
					XmlNodeList nodeList4	= myXmlDoc.GetElementsByTagName("option4");
					//Check if we need to hide the options
					if(!(nodeList4 == null || nodeList4.Count == 0))
					{
						foreach(XmlNode node in nodeList4)
							ddOptions4.Items.Add(node["value"].InnerText);

						ddOptions4.Visible	= true;
						lblOptions.Visible	= true;
					}
				}
				else
				{
					ddOptions1.Items.Clear();
					ddOptions2.Items.Clear();
					ddOptions3.Items.Clear();
					ddOptions4.Items.Clear();
				}
			}
			catch(Exception ex)
			{
                if ( GlobalTraceSwitch.Sw.TraceError )
				    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]Options SetOptionValues ex : {0}", ex),GlobalTraceSwitch.Sw.Info);
                throw;
			}
		}

        #region XML Helper functions

		/// <summary>
		/// provide a string representing an xml file or null for a new document
		/// </summary>
		protected XmlDocument GetOptionsXml()
		{
            if ( TraceProducts.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]Options GetOptionsXml BEG _setOptions: '{0}'.",_setOptions),TraceProducts.Sw.Info);
			XmlDocument myXmlDoc        =   new XmlDocument();
			// read or create document
            if( string.IsNullOrEmpty(_setOptions) )
			{
				XmlElement newRoot		=   myXmlDoc.CreateElement(EComStStr.ProductOptions);
				myXmlDoc.AppendChild(newRoot);	
			}
			else
			{
				myXmlDoc.LoadXml(_setOptions);
			}
			//myXmlDoc is either populated or an empty Xml formated file
			return myXmlDoc;
		}

        #endregion
	}
}